package misc

import (
	"crypto/sha256"
	"encoding/hex"
	"math/rand"
)

func getRandomString(tp int) func(int) string {
	bytes := []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
	if tp == 1 {
		bytes = []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%&*_+")
	}

	return func(length int) string {

		result := make([]byte, length)

		for i := 0; i < length; i++ {
			result[i] = bytes[rand.Intn(len(bytes))]
		}

		return string(result)
	}
}

var randString = getRandomString(0)

//随机字符串
func GetRandomString(length int) string {
	return randString(length)
}

//随机字符串(包括特殊字符)
func GetRandomCharacter(length int) string {
	return getRandomString(1)(length)
}

/**
@note:标准的字符串加密
@param src:目标字符串
@param salt:盐值
*/
func Encrypt(src string, salt string) string {

	hash := sha256.New()

	hash.Write([]byte(src))
	hash.Write([]byte("$"))
	hash.Write([]byte(salt))

	return hex.EncodeToString(hash.Sum(nil))
}
